<?php
defined('BASEPATH') OR exit('No direct script access allowed');


use GuzzleHttp\Client as HttpClient;

class shopify{

    public $checklink=false;
    private $apihost;
    private $apikey;
    private $apipass;
    private $client;
    private $auth;
    private $qcount;
    private $qtime;
    private $Dailyvariant=0;

    public function __construct($param=array())
    {
        $this->apihost = $param["apihost"];
        $this->client = new HttpClient(['timeout'  => 40.0]);
        $this->auth = [$param["apikey"], $param["apipass"],'verify' => false];
        $this->checklink();
    }


    public function change_site($site)
    {
        $extra=json_decode($site["extra"],true);
        $this->apihost=$extra["apihost"];
        $this->auth = [$extra["apikey"], $extra["apipass"],'verify' => false];
        $this->checklink();
    }
    

    public function updatepro($pid,$newdata)
    {
        $path="/admin/api/2019-04/products/".$pid.".json";
        if(isset($newdata["title"]))
        {
            $new=["product"=>["id"=>$pid,"title"=>$newdata["title"]]];
            $this->api_query($path,"put",$new);
            return "ok";            
        }
        if(isset($newdata["price"]))
        {
            $pro=$this->getproduct($pid,"variants");
            if(isset($pro["product"]["variants"]) && count($pro["product"]["variants"])>0)
            {
                $new=["product"=>["id"=>$pid,"variants"=>[]]];
                foreach($pro["product"]["variants"] as $v)
                {
                    //$new["product"]["variants"][]=["id"=>$v["id"],"price"=>$newdata["price"],"compare_at_price"=>$newdata["compare_at_price"]];
                    $new["product"]["variants"][]=["id"=>$v["id"],"price"=>$newdata["price"]];
                }
                $this->api_query($path,"put",$new);
                return "ok";
            }
            else
            {
                return "fail";
            }

        }
    }

    public function syncuser($page,$maxuid)
    {
        $return=["data"=>[]];
        $path="/admin/api/2019-04/customers.json?page=".$page;
        if($maxuid>0)
        {
            $path.="&since_id=".$maxuid;            
        }
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        if(!$result["customers"])
        {
            return $return;
        }
        foreach($result["customers"] as $user)
        {
            $newuser=[
                "id_customer"=>$user["id"],
                "firstname"=>$user["first_name"],
                "lastname"=>$user["last_name"],
                "email"=>$user["email"],
                "date_add"=>date("Y-m-d H:i:s",strtotime($user["created_at"]))
            ];
            $return["data"][]=$newuser;
        }
        return $return;
    }

    public function cancel($oid)
    {
        $path="/admin/api/2019-04/orders/".$oid."/cancel.json";
        $this->api_query($path,"post");
        $this->refund($oid);        
        return "ok";
    }

    private function orderinfo($orderid)
    {
        $path="/admin/api/2019-04/orders/{$orderid}.json";
        $info=$this->api_query($path);
        return json_decode($info,true);
    }

    private function refundcalculate($orderid,$orderinfo)
    {
        $path="/admin/api/2019-04/orders/{$orderid}/refunds/calculate.json";
        $data=["refund"=>[
            "currency"=>"USD",
            "shipping"=>["full_refund"=>"true"],
            "refund_line_items"=>[],
        ]];
        
        foreach($orderinfo["order"]["line_items"] as $item)
        {
            $data["refund"]["refund_line_items"][]=[
                "line_item_id"=>$item["id"],
                "quantity"=>$item["quantity"],
                "restock_type"=>"no_restock",
            ];
        }
        $result=$this->api_query($path,"post",$data);
        return json_decode($result,true);
    }

    private function refund($orderid)
    {
        $orderinfo=$this->orderinfo($orderid);
        $path="/admin/api/2019-04/orders/{$orderid}/refunds.json";
        $data=["refund"=>[
            "currency"=>"USD",
            "notify"=>"true",
            "note"=>"out of stock",            
            "shipping"=>["full_refund"=>"true"],
            "refund_line_items"=>[],
            "transactions"=>[]
        ]];
        foreach($orderinfo["order"]["line_items"] as $item)
        {
            $data["refund"]["refund_line_items"][]=[
                "line_item_id"=>$item["id"],
                "quantity"=>$item["quantity"],
                "restock_type"=>"no_restock",
            ];
        }
        $transactions=$this->refundcalculate($orderid,$orderinfo);
        $data["refund"]["transactions"][]=[
            "parent_id"=>$transactions["refund"]["transactions"][0]["parent_id"],
            "amount"=>$transactions["refund"]["transactions"][0]["amount"],
            "kind"=>"refund",
            "gateway"=>$transactions["refund"]["transactions"][0]["gateway"]
        ];
        $result=$this->api_query($path,"post",$data);
        return json_decode($result,true);
    }

    
    public function fixshiporder($oid,$tracknumber)
    {
        $path="/admin/api/2019-07/orders/".$oid.".json";
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        
        if(!$result["order"]["fulfillment_status"] && $result["order"]["fulfillments"] && count($result["order"]["fulfillments"])>0 && $result["order"]["fulfillments"][0]["service"]=="oberlo")
        {
            $fid =  $result["order"]["fulfillments"][0]["id"];
            $path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid.".json";
            $data=[
                "fulfillment"=>[
                    "id"=>$fid,
                    "tracking_number"=>$tracknumber,
                    "tracking_urls"=>[
                        "https://t.17track.net/en#nums=".$tracknumber
                    ],
                    "notify_customer"=>"true",                    
                ]
            ];
            $result = $this->api_query($path,"put",$data);

            $path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid."/complete.json";
            $result = $this->api_query($path,"post");
            return "ok";
        }
        else
        {
            return "no";
        }
        
    }


    public function shiporder($oid,$tracknumber,$newship=0)
    {
        $path="/admin/api/2019-04/locations.json";
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        
        if(empty($result["locations"]))
        {
            return "nolocation";
        }
        foreach($result["locations"] as $loc)
        {
            if(strtolower($loc["name"])=="oberlo")
            {
                $obid=$loc["id"];
                continue;
            }
            $locationid=$loc["id"];
        }
        $path="/admin/api/2019-07/orders/".$oid.".json";
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        $hasfromob=false;
        $ownship=false;
        $oblines=[];
        $ownlines=[];
        foreach($result["order"]["line_items"] as $item)
        {
             if(isset($item["fulfillment_service"]) && $item["fulfillment_service"]=="oberlo")
             {
                $hasfromob=true;
                $oblines[]=["id"=>$item["id"]];
             }
             else
             {
                $ownship=true;
                $ownlines[]=["id"=>$item["id"]];
             }
        }

        /*
        if(isset($result["order"]["line_items"][0]["fulfillment_service"]) &&$result["order"]["line_items"][0]["fulfillment_service"]=="oberlo")
        {
            $locationid=$obid;
            $fromob=true;
        } 
        */
        
        //if($result["order"]["fulfillments"] && count($result["order"]["fulfillments"])>0 && $result["order"]["fulfillments"][0]["service"]=="oberlo")
        if($result["order"]["fulfillments"] && count($result["order"]["fulfillments"])>0 && $newship==0)
        {   
            foreach($result["order"]["fulfillments"] as $fitem)
            {
                $fid=$fitem["id"];
                $path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid.".json";
                $data=[
                    "fulfillment"=>[
                        "id"=>$fid,
                        "tracking_number"=>$tracknumber,
                        "tracking_urls"=>[
                            $this->gettrackurl($tracknumber)
                        ],
                        "notify_customer"=>"true",                    
                    ]
                ];
                $shiped=false;
                if(isset($fitem["status"]) && $fitem["status"]=="success")
                {
                    $data["fulfillment"]["notify_customer"]="false";
                    $shiped=true;
                }else if(isset($result["order"]["fulfillment_status"]) && $result["order"]["fulfillment_status"]=="fulfilled")
                {
                	$data["fulfillment"]["notify_customer"]="false";
                    $shiped=true;
                }
                $result = $this->api_query($path,"put",$data);
                if(!$shiped)
                {
                    $path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid."/complete.json";
                    $result = $this->api_query($path,"post");
                }

            }      
            /*
            $fid =  $result["order"]["fulfillments"][0]["id"];
            
			$shiped=false;
			if((isset($result["order"]["fulfillment_status"]) && $result["order"]["fulfillment_status"]=="fulfilled") or (isset($result["order"]["fulfillments"][0]["status"]) && $result["order"]["fulfillments"][0]["status"]=="success")){
				$shiped=true;
			}
            if($shiped)
            {
                $data["fulfillment"]["notify_customer"]="false";
            }
            $result = $this->api_query($path,"put",$data);
            
            if(!$shiped)
            {
            	$path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid."/complete.json";
        		$result = $this->api_query($path,"post");
            }
            */
        }
        else
        {
            if($ownship)
            {
                $path="/admin/api/2019-04/orders/".$oid."/fulfillments.json";
                $data=[
                    "fulfillment"=>[
                        "location_id"=>$locationid,
                        "tracking_number"=>$tracknumber,
                        "tracking_urls"=>[
                            $this->gettrackurl($tracknumber)
                        ],
                        "notify_customer"=>"true"
                    ]
                ];
                if($hasfromob)
                {
                    $data["fulfillment"]["line_items"]=$ownlines;
                }
                $result = $this->api_query($path,"post",$data);
            }
    
            if($hasfromob)
            {
                $path="/admin/api/2019-04/orders/".$oid."/fulfillments.json";
                $data=[
                    "fulfillment"=>[
                        "location_id"=>$obid,
                        "tracking_number"=>$tracknumber,
                        "tracking_urls"=>[
                            $this->gettrackurl($tracknumber)
                        ],
                        "notify_customer"=>"true"
                    ]
                ];
                if($ownship)
                {
                    $data["fulfillment"]["line_items"]=$oblines;
                    //$data["fulfillment"]["tracking_number"]=null;
                }
                $result = $this->api_query($path,"post",$data);

                $result=json_decode($result,true);
            	$fid=$result["fulfillment"]["id"];
            	$path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid."/complete.json";
                $result = $this->api_query($path,"post");
                
            }

        }
            /*
            if($fromob)
            {
            	$result=json_decode($result,true);
            	$fid=$result["fulfillment"]["id"];
            	$path = "/admin/api/2019-04/orders/".$oid."/fulfillments/".$fid."/complete.json";
            	$result = $this->api_query($path,"post");
            }
            */
        return "ok";
    }

    private function gettrackurl($tracknumber)
    {
        $check=strtolower($tracknumber);
        if(substr($check,0,2)=="yt")
        {
            return "http://www.yuntrack.com/Track/Detail/".$tracknumber;
        }
        /*
        else if(substr($check,0,2)=="sf")
        {
            return "https://www.sf-express.com/us/en/dynamic_function/waybill/#search/bill-number/".$tracknumber;
        }
        */
        else
        {
            return "https://t.17track.net/en#nums=".$tracknumber;
        }

    }

    public function checkwaitorder($waitorder)
    {
        $returnorder=[];
        $checkid=[];
        $indexorder=[];
        foreach($waitorder as $wo)
        {   
            $indexorder[$wo["oid"]]=$wo;
            $checkid[]=$wo["oid"];
        }
        $rstring=implode(",",$checkid);
        if($rstring)
        {
            $path="/admin/api/2019-04/orders.json?limit=250&fields=id&status=open&financial_status=paid&ids=".$rstring;
            $result=$this->api_query($path,"get");
            $result=json_decode($result,true);
            if(!$result["orders"])
            {
                return $returnorder;
            }
            foreach($result["orders"] as $order)
            {
                $returnorder[]=$indexorder[$order["id"]];
            }
        }
        return $returnorder;
    }

    public function syncorder($page,$maxoid=0,$state=1)
    {
        $return=["data"=>[]];
        if($state==1)
        {
            $s="paid";
        }
        else
        {
            $s="pending";
        }
        $path="/admin/api/2019-04/orders.json?page=".$page."&status=open&financial_status=".$s;
        if($maxoid>0)
        {
            $path.="&since_id=".$maxoid;
        }
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        if(!$result["orders"])
        {
            return $return;
        }
        foreach($result["orders"] as $order)
        {
            $neworder=[
                "id_order"=>$order["id"],
                "id_customer"=>$order["customer"]["id"],
                "current_state"=>2,
                "reference"=>$order["order_number"],
                "total_paid"=>$order["total_price"],
                "firstname"=>$order["shipping_address"]["first_name"],
                "lastname"=>$order["shipping_address"]["last_name"],
                "phone"=>$order["shipping_address"]["phone"],
                "phone_mobile"=>"",
                "name"=>$order["shipping_address"]["country"],
                "iso_code"=>$order["shipping_address"]["country_code"],
                "stname"=>$order["shipping_address"]["province"],
                "postcode"=>$order["shipping_address"]["zip"],
                "stcode"=>$order["shipping_address"]["province_code"],
                "city"=>$order["shipping_address"]["city"],
                "address1"=>$order["shipping_address"]["address1"],
                "address2"=>isset($order["shipping_address"]["address2"])?$order["shipping_address"]["address2"]:"",
                "date_add"=>date("Y-m-d H:i:s",strtotime($order["created_at"])),
                "payment"=>$order["payment_gateway_names"][0],
                "products"=>[],
            ];

            $path="/admin/api/2019-04/orders/".$order["id"]."/transactions.json";
            $transaction=$this->api_query($path,"get");
            $transaction=json_decode($transaction,true);
            if(isset($transaction["transactions"]) && $transaction["transactions"]){
                $neworder["payment"].=":".$transaction["transactions"][0]["authorization"];
            }

            foreach($order["line_items"] as $item)
            {
                if(empty($item["product_id"]))
                {
                    $newp=[
                        "product_name"=>$item["title"],                    
                        "variant"=>$item["variant_title"],
                        "product_quantity"=>$item["quantity"],
                        "product_price"=>$item["price"],
                        "product_id"=>$item["product_id"],
                        "image"=>"",
                        "handler"=>"",
                    ];
                }
                else
                {
                    $hand=$this->getproduct($item["product_id"],"handle,images");
                    $newp=[
                        //"product_name"=>$item["name"],
                        "product_name"=>$item["title"],                    
                        "variant"=>$item["variant_title"],
                        "product_quantity"=>$item["quantity"],
                        "product_price"=>$item["price"],
                        "product_id"=>$item["product_id"],
                        "image"=>$hand["product"]["images"][0]["src"],
                        "handler"=>"/products/".$hand["product"]["handle"],
                    ];
                    foreach($hand["product"]["images"] as $img)
                    {
                        if(in_array($item["variant_id"],$img["variant_ids"]))
                        {
                            $newp["image"]=$img["src"];
                            break;
                        }
                    }
                }
                $neworder["products"][]=$newp;
                sleep(1);
            }
            $return["data"][]=$neworder;
        }
        return $return;
    }

    private function getproduct($pid,$fields="")
    {
        $path="/admin/api/2019-04/products/".$pid.".json";
        if($fields)
        {
            $path.="?fields=".$fields;
        }
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        return $result;
    }

    public function delproduct($pro)
    {
        $pid=$pro["rid"];
        $path="/admin/api/2019-04/products/".$pid.".json";
        $this->api_query($path,"delete");
        return "ok";
    }

    public function testproduct($product)
    {

        $formatproduct=$this->format_product($product);       
        return $formatproduct;
    }

    public function addproduct($product)
    {
        $this->savelog("strat add product".$product["name"]);
        $this->qcount=0;
        $catid = $product["rid"];

        $formatproduct=$this->format_product($product);        
        $path="/admin/products.json";
        $result=$this->api_query($path,"post",$formatproduct);
        if(!$result && $this->Dailyvariant==1)
        {
            $this->savelog("reeach daily limit");
            return ["limit"=>1];
        }
        $result=json_decode($result,true);        
        $this->savelog("add base product");
        
        $pid = $result['product']['id'];
        $handle = $result['product']['handle'];
        $this->addcollect($catid,$pid);
        $checkva=[];
        foreach($product["option"]["vv"] as $val)
        {
            if(isset($val["image"]) && $val["image"])
            {
                foreach($val["value"] as $k=>$v)
                {
                    if($k<count($val["image"]))
                    {
                        $checkva[]=["val"=>$v,"src"=>$this->_refix_ali_imgurl($val["image"][$k]),"vid"=>[]];
                    }
                }
                break;
            }
        }
        /*
        if(isset($product["option"]["vv"][0]) && isset($product["option"]["vv"][0]["image"]) && $product["option"]["vv"][0]["image"])
        {
            $i=0;
            foreach($product["option"]["vv"][0]["value"] as $k=>$v)
            {
                if($k<count($product["option"]["vv"][0]["image"]))
                {
                    $checkva[]=["val"=>$v,"src"=>$this->_refix_ali_imgurl($product["option"]["vv"][0]["image"][$k]),"vid"=>[]];
                }
            }

        }*/

        $imgs=$result['product']['images'];
        $key = array_rand($imgs,1);
        $key = 0;
        $randimgid=$imgs[$key]["id"];
        $randvid=[];
        $catch=0;
        foreach($result['product']['variants'] as $v)
        {
            $match=0;
            if($checkva)
            {
                foreach($checkva as $k=>$va)
                {
                    if($v["option1"]==$va["val"]||$v["option2"]==$va["val"]||$v["option3"]==$va["val"])
                    {
                        $checkva[$k]["vid"][]=$v["id"];
                        $match=1;
                        $catch=1;
                        break;
                    }                        
                }
                if($match==0)
                {
                    $randvid[]=$v["id"];
                }
            }
            else
            {
                $randvid[]=$v["id"];
            }
        }
        if($catch==1)
        {
            $this->addvaimg($checkva,$pid);
        }
        elseif($randvid)
        {
            $this->addrandvaimg($randvid,$pid,$randimgid);
        }

        $this->savelog("end add product ,query times:".$this->qcount);
        return ["rid"=>$pid,"url"=>"/products/".$handle];
    }

    private function addcollect($collectid,$pid)
    {
        $path="/admin/api/2020-04/collects.json";
        $data=[
            "collect"=>[
                "product_id"=>$pid,
                "collection_id"=>$collectid,
            ]
        ];
        $this->api_query($path,"post",$data);
    }


    private function addvaimg($checkva,$pid)
    {
        $path="/admin/products/".$pid."/images.json";
        if($checkva)
        {
            foreach($checkva as $item)
            {
                if($item["vid"])
                {
                    $img=["image"=>["src"=>$item["src"],"variant_ids"=>$item["vid"]]];
                    $this->api_query($path,"post",$img);
                }
    
            }    
        }

    }

    private function addrandvaimg($randvid,$pid,$randimgid)
    {
        $path="/admin/products/".$pid."/images/".$randimgid.".json";
        $img=["image"=>["id"=>$randimgid,"variant_ids"=>$randvid]];
        $this->api_query($path,"put",$img);        
    }

    private function _refix_ali_imgurl($url)
    {
        if(strstr($url,"cdn.shopify.com"))
        {
            return $url;
        }
        $url = str_replace(["ae01.alicdn.com","_50x50.jpg"],["picture.qdbeauty.com","_640x640.jpg"],$url);
        if(strstr($url,"_640x640"))
        {
            $tmp=explode("_640x640",$url);
            $url=$tmp[0];
        }
        return $url;
    }

    private function format_product($product)
    {
        $newproduct=array("product"=>array());
        $newproduct["product"]["title"]=$product["name"];
        /*
        if($product["source"]=="aliexpress" && $product["meta"])
        {
            $extra=json_decode($product["meta"],true);
            $newproduct["product"]["metafields_global_description_tag"]=$extra["meta"];  
            $tags=explode(",",$extra["keyword"]);
            $newtag=[];
            foreach($tags as $v)
            {
                if(trim($v))
                {
                    $newtag[]=trim($v);                    
                }
            }          
            $newproduct["product"]["tags"]=implode(",",$newtag);
        }
        else
        {
            $newproduct["product"]["metafields_global_description_tag"]=$product["meta"]?$product["meta"]:$product["name"];

            if($product["tags"])
            {
                $newproduct["product"]["tags"]=substr(rtrim($product["tags"],","),0,200);
                $tmp_tag=explode(",",$newproduct["product"]["tags"]);
                array_pop($tmp_tag);
                $newtag=[];
                foreach($tmp_tag as $tag)
                {
                    $tag=str_replace(["-","%","&","#"],"",$tag);
                    
                    if(empty(trim($tag))) continue;
                    if(strstr($tag,"#")) continue;
                    if(strstr($tag,"M/L/")) continue;
                    if(strstr($tag,"M/L/")) continue;
                    if(preg_match('/china/i',$tag)) continue;
                    if(!in_array($tag,$newtag)) $newtag[]=$tag;
                }
                $newproduct["product"]["tags"]=implode(",",$newtag);
               
            }
        }
        */
        $newproduct["product"]["body_html"]=$product["desc"];
        if(empty($product["desc"]))
        {
            $newproduct["product"]["body_html"]=$product["name"];
        }
        else
        {
            $newproduct["product"]["body_html"]=$product["desc"];
        }
        //$newproduct["product"]["vendor"]=$product["vendor"];
        //$newproduct["product"]["product_type"]=$product["catename"];
        $newproduct["product"]["product_type"]=$product["vendor"];
        $newproduct["product"]["variants"]=array();
        $newproduct["product"]["options"]=array();

        $newproduct["product"]["images"]=array();
       

        
        //$compare_ratio=[1.3,1.35,1.4,1.45,1.5,1.55,1.6,1.65,1.7];

        if($product["option"] && isset($product["option"]["vv"]))
        {
            
            foreach($product["option"]["vv"] as $k=>$v)
            {
                $newproduct["product"]["options"][]=array("name"=>$v["name"],"values"=>$v["value"]);
            }
            
            if(count($product["option"]["vv"])==1)
            {
                $icount=0;
                $val_cons=array();
                foreach($product["option"]["vv"][0]["value"] as $k=>$val)
                {

                    $price=$product["price"];
                    if(isset($product["option"]["vv"][0]["price"][$k]))
                    {
                        $price=$product["option"]["vv"][0]["price"][$k];
                    }
                    //shuffle($compare_ratio);
                    //$compareprice=sprintf("%.2f",$price*$compare_ratio[0]);
                    //$newproduct["product"]["variants"][]=array("option1"=>$val,"price"=>$price,	"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","compare_at_price"=>$compareprice,"sku"=>$product["sku"]."-".$icount);
                    $newproduct["product"]["variants"][]=array("option1"=>$val,"price"=>$price,	"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","sku"=>$product["sku"]."-".$icount);
                    $icount++;
                    if($icount>100){
                       	break;
                    }
                }
            }
            else if(count($product["option"]["vv"])==2)
            {   
                $icount=0;
                $val_cons=array();
                foreach($product["option"]["vv"][0]["value"] as $k1=>$v1)
                {
                    //shuffle($compare_ratio);
                    
                    foreach($product["option"]["vv"][1]["value"] as $k2=>$v2)
                    {
                        if($icount>99){
                            break;
                        }
                        $price=$product["price"];
                        foreach([0,1] as $k)
                        {
                            if(isset($product["option"]["vv"][$k]["price"][${'k'.($k+1)}]))
                            {
                                $price=$product["option"]["vv"][$k]["price"][${'k'.($k+1)}];
                                break;
                            }
                        }
                        if($product["option"]["newsku"])
                        {
                        
                            foreach($product["option"]["newsku"] as $sku)
                            {
                                if($sku["val"] && count($sku["val"])==2)
                                {
                                    if($v1==$sku["val"][0]&&$v2==$sku["val"][1])
                                    {
                                        $price=$sku["price"];
                                        break;
                                    }
                                }
                            }
                        }

                        //$compareprice=sprintf("%.2f",$price*$compare_ratio[0]);
                        //$variant=array("option1"=>$v1,"option2"=>$v2,"price"=>$price,"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","compare_at_price"=>$compareprice,"sku"=>$product["sku"]."-".$icount);
                        $variant=array("option1"=>$v1,"option2"=>$v2,"price"=>$price,"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","sku"=>$product["sku"]."-".$icount);
                        $newproduct["product"]["variants"][]=$variant;
                        $icount++;
                        
                    }                   
                }
            }
            else if(count($product["option"]["vv"])==3)
            {   
                $icount=0;
                $val_cons=array();
                foreach($product["option"]["vv"][0]["value"] as $k1=>$v1)
                {
                    foreach($product["option"]["vv"][1]["value"] as $k2=>$v2)
                    {
                        //shuffle($compare_ratio);
                        foreach($product["option"]["vv"][2]["value"] as $k3=>$v3)
                        {
                            if($icount>99){
                                break;
                            }
                            $price=$product["price"];
                            foreach([0,1,2] as $k)
                            {
                                if(isset($product["option"]["vv"][$k]["price"][${'k'.($k+1)}]))
                                {
                                    $price=$product["option"]["vv"][$k]["price"][${'k'.($k+1)}];
                                    break;
                                }
                            }
                            if($product["option"]["newsku"])
                            {
                                foreach($product["option"]["newsku"] as $sku)
                                {
                                    if($sku["val"] && count($sku["val"])>=3)
                                    {
                                        if($v1==$sku["val"][0]&&$v2==$sku["val"][1]&&$v3==$sku["val"][2])
                                        {
                                            $price=$sku["price"];
                                            break;
                                        }
                                    }
                                }
                            }                            
                            //$compareprice=sprintf("%.2f",$price*$compare_ratio[0]);
                            //$variant=array("option1"=>$v1,"option2"=>$v2,"option3"=>$v3,"price"=>$price,"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","compare_at_price"=>$compareprice,"sku"=>$product["sku"]."-".$icount);
                            $variant=array("option1"=>$v1,"option2"=>$v2,"option3"=>$v3,"price"=>$price,"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","sku"=>$product["sku"]."-".$icount);
                            $newproduct["product"]["variants"][]=$variant;
                            $icount++;
                        }
                    }                   
                }
            }
        }
        else
        {            
            $price=$product["price"];
            //shuffle($compare_ratio);
            //$compareprice=sprintf("%.2f",$price*$compare_ratio[0]);

            //$newproduct["product"]["variants"][]=array("option1"=>"Default Title","price"=>$price,"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","compare_at_price"=>$compareprice,"sku"=>$product["sku"].$product["tbid"]);
            $newproduct["product"]["variants"][]=array("option1"=>"Default Title","price"=>$price,"inventory_management"=>"shopify","inventory_policy"=>"continue","inventory_quantity"=>1000,"taxable"=>"false","sku"=>$product["sku"].$product["tbid"]);
        }

        if($product["thumb"])
        {
            $icount=0;
            foreach($product["thumb"] as $img)
            {
                $newproduct["product"]["images"][]=array("src"=>$this->imgurl($img));
                /*
                $icount++;
                if(count($product["thumb"])>1 && $icount==1)
                {
                    $firstimg=array("src"=>$this->imgurl($img));
                    continue;
                }
                $newproduct["product"]["images"][]=array("src"=>$this->imgurl($img));
                if(count($product["thumb"])>1 && $icount==2)
                {
                    $newproduct["product"]["images"][]=$firstimg;
                }*/
            }
        }
        
        return $newproduct;
    }

    private function imgurl($url)
    {
        if(substr($url,0,4)!="http")
        {
            $url="http:".$url;
        }

        $url = str_replace("ae01.alicdn.com","picture.qdbeauty.com",$url);
        $url = str_replace("_50x50","_640x640",$url);
        if(strstr($url,"_640x640"))
        {
            $tmp=explode("_640x640",$url);
            return $tmp[0];
        }
        else
        {
            return $url;
        }        
    }


    public function synccatrgory($category)
    {
        $result=[];
        foreach($category as $cat)
        {
            if($cat["rid"]>0)
            {
                $path = "/admin/api/2019-04/custom_collections/".$cat["rid"].".json";
                $info = $this->api_query($path,"get");
                if(is_array($info))
                {
                    $this->updatecollect($cat["rid"],$cat["name"]);
                    continue;
                }
            }
            
            $rid=$this->publishcat($cat["name"]);
            $result[$cat["id"]]=$rid;
            
        }
        return $result;
    }

    public function delcatgory($cat)
    {
        $rid=$cat["rid"];
        if($rid>0)
        {
            $path="/admin/api/2019-04/custom_collections/".$rid.".json";
            $this->api_query($path,"delete");
        }
    }

    private function updatecollect($rid,$name)
    {
        $path="/admin/api/2019-04/custom_collections/".$rid.".json";
        $data=["custom_collections"=>["id"=>$rid,"title"=>$name]];
        $this->api_query($path,"put",$data);        
    }

    private function publishcat($name)
    {
        $path="/admin/api/2019-04/custom_collections.json?title=".$name;
        $result=$this->api_query($path,"get");
        $result=json_decode($result,true);
        if($result["custom_collections"] && count($result["custom_collections"])>0)
        {
            return $result["custom_collections"][0]["id"];
        }
        $path="/admin/api/2019-04/custom_collections.json";
        $data=["custom_collection"=>["title"=>$name]];
        //$data=["custom_collections"=>["title"=>$name,"rules"=>[["column"=>"title","relation"=>"contains","condition"=>strtolower($name)]]]];
        $result=$this->api_query($path,"post",$data);
        $result=json_decode($result,true);
        return $result["custom_collections"]["id"];
    }

    private function api_query($path,$querymethod="get",$jsondata=[])
    {
        //$host="https://".$this->apikey.":".$this->apipass."@".$this->apihost;
        $host="https://".$this->apihost;
        $url=$host.$path;
        $errorcount=1;
        $maxerror=$errorcount;
        while($errorcount>0)
        {
            try
            {
                while(1)
                {
                    $this->qcount++;
                    switch($querymethod)
                    {
                        case "get":
                            $res=$this->client->request('GET', $url, [
                                'auth' => $this->auth
                            ]);
                            $html_doc=$res->getBody();
                            break;
                        case "post":
                            $res=$this->client->request('POST', $url, [
                                'json' => $jsondata,
                                'auth' => $this->auth
                            ]);
                            $html_doc=$res->getBody();
                            break;
                        case "put":
                            $res=$this->client->request('PUT', $url, [
                                'json' => $jsondata,
                                'auth' => $this->auth
                            ]);
                            $html_doc=$res->getBody();                        
                            break;
                        case "delete":
                            $res=$this->client->request('DELETE', $url, [
                                'auth' => $this->auth
                            ]);
                            $html_doc=$res->getStatusCode();
                            break;
                    }

                    if(strstr($html_doc,"Exceeded 2 calls per second"))
                    {

                        $this->savelog("api Exceeded  ".$html_doc);

                        sleep(1);
                        continue;
                    }
                    else
                    {
                        return $html_doc;                        
                    }
                }
            }   
            //catch (Exception $e) {
            catch (\GuzzleHttp\Exception\ClientException $e){
                $this->savelog("api error ".$e->getResponse()->getBody()->getContents());
                //$this->savelog("api error ".$e->getMessage());
                //echo $url."\n";
                $this->savelog('请求异常，重试...'.($maxerror-$errorcount+1).'/'.$maxerror.'...');
                $this->savelog($e->getMessage());
                //echo "\n";
                $errormsg = $e->getMessage();
                if(strstr($e->getMessage(),"Too Many Requests"))
                {
                    if(strstr($e->getMessage(),"Daily variant creation limit reached"))
                    {
                        $this->Dailyvariant=1;
                        return;
                    }
                    sleep(mt_rand(1,5));
                }
                else
                {
                    $errorcount--;
                }
            }
        }
        return $errormsg;
    }

    private function savelog($msg,$datainfo=[])
    {
        if(!file_exists("./logs/".date("Ymd")."shopify.text"))
        {
			file_put_contents("./logs/".date("Ymd")."shopify.text","newfile\n",FILE_APPEND | LOCK_EX);
        	chown("./logs/".date("Ymd")."shopify.text","www");
        }
        
        $rawmsg="-----time:".date("H:i:s")."---------\n";
        $rawmsg.=$msg."\n";
        if($datainfo) $rawmsg.=json_encode($datainfo)."\n\n";
        file_put_contents("./logs/".date("Ymd")."shopify.text",$rawmsg,FILE_APPEND | LOCK_EX);
    }

    private function checklink()
    {
        $path="/admin/api/2019-04/shop.json";
        $data=$this->api_query($path);
        $result=json_decode($data,true);
        if($result["shop"]["id"])
        {
            $this->checklink=true;
        }
    }
    
}
